//
//  MainViewController.swift
//  WeiboSwift
//
//  Created by Apple13 on 16/7/9.
//  Copyright © 2016年 yupeng. All rights reserved.
//

import UIKit
import SVProgressHUD


class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        //设置tabBar为自定义的tabbar
            //创建自定义的额tabBar
            let wbTabBar = WBTabBar()
        
            //设置代理--- 要添加分类遵守代理
            wbTabBar.Delegate = self
        
            //设置闭包
            wbTabBar.composeButtonclosure = { [weak self] in
            
                //验证是否登录
                if !UserAccountViewModel.shareUserAccount.isLogin{
                    
                    SVProgressHUD.showErrorWithStatus("请先登录", maskType: .Gradient)
                    
                    return
                }
                //执行到此表示已经登录
                let composeView = ComposeView()
                
                //获取最上面的一个Window
                let window = UIApplication.sharedApplication().windows.last!
                window.addSubview(composeView)
                
                
            print("➕闭包弄过来的")
            }
        
        //用kvc给属性赋值把系统自带的tabbar换成自定义的
        setValue(wbTabBar, forKey: "tabBar")
        
        
        self.view.backgroundColor = UIColor.brownColor()
        
        //添加子控制器
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName:  "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
////MARK: - ③懒加载中间的"➕"按钮
//   lazy var composeButton: UIButton = {
//        let button = UIButton()
//        
//        //给按钮添加点击事件
//        
//        
//        return button
//    }
    
    
//MARK: - 按钮的点击事件
    @objc private func composeButtonClick(){
        print("点了一下")
    }
    
    
    
    
//MARK: -  ①添加子控制器
    
    private  func addChildViewController() {
        tabBar.tintColor = UIColor.orangeColor()
        
        let vc = HomeTableViewController()
        vc.title = "首页"
        vc.tabBarItem.image = UIImage(named: "tabbar_home")
        
        let nav = UINavigationController(rootViewController:vc)
        
        addChildViewController(nav)
    }

///Users/yp-apple/Documents/代码/2016年7月代码/WeiboSwift/WeiboSwift/WeiboSwift/Assets.xcassets/TabBar/allproducts.imageset/allproducts@2x.png
   
    
//MARK: - ②抽象出一个方法添加多个控制器
    
    
    private func addChildViewController(vc: UIViewController,title:String,imageName:String) {
      
        
        
//‼️设置自定义的tabBarItem  -- begin - 可选执行--这个一定要放在最前面
        
        vc.tabBarItem = WBTabBarItem()
        
//‼️设置自定义的tabBarItem  -- end
        
        //设置标题
        vc.title = title
        
        //设置背景图片
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")
        
        //设置选中文字的颜色
        let textColorAttr = [
            NSForegroundColorAttributeName: UIColor.orangeColor()
        ]
        vc.tabBarItem.setTitleTextAttributes(textColorAttr, forState: UIControlState.Selected)
        
        
        
        //设置字体大小属性
        let textFontAttr = [
            NSFontAttributeName :UIFont.systemFontOfSize(13)
        ]
        
        vc.tabBarItem.setTitleTextAttributes(textFontAttr, forState: UIControlState.Normal)
        
        //包装到导航控制器里面
        let nav = UINavigationController(rootViewController:vc)
        addChildViewController(nav)
        
        
    }
    
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

  
    
}


extension MainViewController: WBTabBarDelegate{
    func didSelectedComposeButton(){
        print("➕代理对象调用过来的")
    }
}
